//
//  HCFindHighPraiseView.m
//  Aierle
//
//  Created by jiayibai-02por on 2017/6/1.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCFindHighPraiseView.h"
#import "HighPraiseCell.h"

@interface HCFindHighPraiseView()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *highPraiseCollectionView;

@end

@implementation HCFindHighPraiseView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self initSubviews];
    }
    return self;
}

- (void)initSubviews {

    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(10, 0, Screen_width-20, 0.5)];
    line.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:line];
    
    UILabel *leftTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 8, 100, 16)];
    leftTitleLabel.font = [UIFont systemFontOfSize:14];
    leftTitleLabel.textColor = [UIColor blackColor];
    leftTitleLabel.text = @"大家好评";
    [self addSubview:leftTitleLabel];
    
//    UIButton *rightWholeButton = [[UIButton alloc] initWithFrame:CGRectMake(Screen_width - YBKAdaptiveW(65), 0, YBKAdaptiveW(65), YBKAdaptiveH(35))];
//    [rightWholeButton setTitle:@"全部>" forState:UIControlStateNormal];
//    rightWholeButton.titleLabel.font = [UIFont systemFontOfSize:10];
//    [rightWholeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [rightWholeButton addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:rightWholeButton];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    layout.minimumLineSpacing = (Screen_width-YBKAdaptiveW(65)*4)/4;
    layout.minimumInteritemSpacing = 20;
    layout.itemSize = CGSizeMake(YBKAdaptiveW(65), YBKAdaptiveH(65));
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.highPraiseCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, YBKAdaptiveH(35), Screen_width, YBKAdaptiveH(65)) collectionViewLayout:layout];
    self.highPraiseCollectionView.delegate = self;
    self.highPraiseCollectionView.dataSource = self;
    self.highPraiseCollectionView.backgroundColor = [UIColor whiteColor];
    [self.highPraiseCollectionView registerNib:[UINib nibWithNibName:@"HighPraiseCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"highpraise"];
    self.highPraiseCollectionView.showsVerticalScrollIndicator = NO;
    self.highPraiseCollectionView.showsHorizontalScrollIndicator = NO;
    [self addSubview:self.highPraiseCollectionView];
    
//    UIButton *moreButton = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.highPraiseCollectionView.frame), YBKAdaptiveW(85), 40)];
//    moreButton.center = CGPointMake(Screen_width/2.0, moreButton.center.y);
//    [moreButton setTitle:@"查看更多>>>" forState:UIControlStateNormal];
//    [moreButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
//    moreButton.titleLabel.font = [UIFont systemFontOfSize:10];
//    [self addSubview:moreButton];
}

- (void)setHighPraiseArray:(NSArray *)highPraiseArray {

    _highPraiseArray = highPraiseArray;
    if (highPraiseArray.count != 0) {
        
        [self.highPraiseCollectionView reloadData];
    }
}

#pragma mark -- 点击全部
- (void)rightButtonClick:(UIButton *)sender {

    
}

#pragma mark -- UICollectionView delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.highPraiseArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"highpraise";
    HighPraiseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    if (!cell) {
        
        cell = [[HighPraiseCell alloc] initWithFrame:CGRectZero];
    }
    
    if (self.highPraiseArray.count != 0) {
        
        NSString *imageUrl = [NSString stringWithFormat:@"%@",[self.highPraiseArray[indexPath.item] objectForKey:@"path"]];
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:nil];
        
        NSString *cellName = [NSString stringWithFormat:@"%@",[self.highPraiseArray[indexPath.item] objectForKey:@"name"]];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    if (self.didClickCellBlock) {
        
        self.didClickCellBlock([self.highPraiseArray[indexPath.item] objectForKey:@"url"]);
    }
}

@end
